<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class IndexController extends Controller{
	private $cateModel;
	private $goodsModel;
	//构造函数
	public function __init()
	{
		$this->cateModel = new \Common\Model\Category;
		$this->goodsModel = new \Common\Model\Goods;
	}
	
    //动作
    public function index(){
    	$floor = $this->cateModel->where("pid = 0")->field("cid,cname")->get();
		$data = $this->cateModel->get();
//		获得二级分类
		foreach ($floor as $k => $v) {
			/*
			 * 获得两个分类*/
			$v['erji'] = $this->cateModel->where("pid = {$v['cid']}")->field("cid,cname")->limit(2)->get();
			$floor[$k]['erji'] = $v['erji'];
			/*
			 * 获得6个商品*/
			$v['cids'] = $this->cateModel->getSon($data,$v['cid']);
			$v['cids'][]=$v['cid'];
			$floor[$k]['goods'] = $this->goodsModel->where("category_cid in (".implode(',', $v['cids']).")")->field("gid,gname,listpic")->limit(6)->get();
			
			/*
			 * 获得品牌*/
			$v['bid'] = $this->goodsModel->join('brand','bid','=','brand_bid')->where("category_cid in (".implode(',', $v['cids']).")")->lists('bid');
			$v['bid'] = $v['bid']?array_unique($v['bid']):array();
			if($v['bid']){
				$floor[$k]['brand'] = Db::table('brand')->where("bid IN (".implode(',', $v['bid']).")")->limit(6)->get();
			}else{
				$floor[$k]['brand'] = array();
			}
			/*
			 * 获得8个三级分类*/
			$floor[$k]['cate'] = $this->cateModel->where("pid = {$v['erji'][0]['cid']} OR pid ={$v['erji'][1]['cid']}")->field("cid,cname")->limit(8)->get();
			$kk =$k+1;
			$floor[$k]['bigpic'] = "Upload/logo/floor".$kk.".jpg";
		}
		
		
//		p($floor);
		View::with('floor',$floor);
    	View::make();
    }
}
